import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Questions are represented by events that are given a lower priority 
 * than meetings. Besides that, Questions are handeled by the scheduler 
 * as if they are a meeting. Questions are randomly going to
 * either be answered on the spot by the team lead or be turned into an
 * UnansweredQuestion if the asker is a developer (as opposed to TeamLead).
 * When the question is 'run'(implemented in Meeting) the extended 
 * onMeetingStart is called to decide who can answer the question.
 *
 */
public class Question extends Meeting
{

	private Random answers;
	Schedule schedule;
	private Employee asker;
	private TeamLead leader;
	private Manager manager;
	private int LeadAnswers;

	public Question(Time eventTime, List<Employee> members, Schedule scheduler, Employee asker, TeamLead lead, Manager manager)
	{
		super(eventTime, members, new TimeDuration(0, 0), (asker.toString() +"'s QUESTION"));
		priority = 0;
		answers = new Random();
		this.schedule = scheduler;
		this.asker = asker;
		this.leader = lead;
		this.manager = manager;
	}

	
	
	protected void onEmployeeFinishedMeeting(Employee employee)
	{

	}

	protected void onEmployeeStartedMeeting(Employee employee)
	{
		// TODO: When the TeamLead shows up, randomly determine if he can answer

	}

	protected void onMeetingFinished()
	{
		String message;
		if(LeadAnswers == 1){
			message = (asker.getSchedule().getCurrentTime().toString() + " " + asker.toString()
					+ "'s QUESTION needs to be asked to the Manager.");
		}
		else{
			message = (asker.getSchedule().getCurrentTime().toString() + " " + asker.toString()
					+ " had their QUESTION answered.");
		}
		PrintBuffer.printMessage(getEventTime(), message);

	}

	protected void onMeetingStarted()
	{
		
		
		// LeadAnswers == 0, Lead will answer
		// LeadAnswers == 1, Manager is needed to answer
		LeadAnswers = answers.nextInt(2);
		
		// If the person asking the question is the TeamLead then TeamLead
		// can't answer the question.
		if(asker instanceof TeamLead) LeadAnswers = 1;

		if (LeadAnswers == 0) // The question is answered immediately on arrival by the TeamLead
		{

//			
//			String message = (this.getEventTime().toString() + " " + asker.toString()
//					+ " had a question answered.");
//			PrintBuffer.printMessage(getEventTime(), message);

		}
		else // The TeamLead can't answer the question
		{
			// A new event with the manager added into the members is needed
			List<Employee> newMembers = new ArrayList<Employee>(); 
			newMembers.add(asker);
			newMembers.add(leader);
			newMembers.add(manager);
			if(asker == null || leader == null || manager == null) //DEBUG
				System.out.println("one of these things is null"); //DEBUG
			UnansweredQuestion askManager = new
					UnansweredQuestion(asker.getSchedule().getCurrentTime(),newMembers);
			QuestionThought toManager = new QuestionThought(asker.getSchedule().getCurrentTime(), (Developer)asker, leader, manager);
			asker.getSchedule().scheduleEvent(toManager);
		}
	}
	
	public void retryQuestion(){
		barrier.reset();
	}

}
